package com.example.paper.mvp.presenter;

import android.Manifest;
import android.content.pm.PackageManager;

import androidx.core.app.ActivityCompat;

import com.blankj.utilcode.util.PhoneUtils;
import com.example.mvp.model.BaseEntity;
import com.example.mvp.presenter.BasePresenter;
import com.example.paper.app.App;
import com.example.paper.brean.ResultBean;
import com.example.paper.http.logUtils.SpUtils;
import com.example.paper.mvp.contract.AllContract;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;

public class AllPresenter extends BasePresenter<AllContract.AllModel,AllContract.AllView>{
    public AllPresenter(AllContract.AllModel mModel, AllContract.AllView mView) {
        super(mModel, mView);
    }

    //请求服务器token方法
    public void requestToken() {
        Map<String, Object> map = new HashMap<>();
        if (ActivityCompat.checkSelfPermission(App.getInstance().getApplicationContext(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        String imei = PhoneUtils.getIMEI();
        map.put("imie",""+imei);
        mModel.getTokent(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::onNext);
    }

    @Override
    public void onNext(@NonNull BaseEntity baseEntity) {
        //请求成功逻辑校验
        ResultBean entity = (ResultBean) baseEntity;
        if (entity.getStatus().equals("200")){
            //保存token到本地sp存储
            SpUtils.saveData("token",entity.getValues().getToken());
            //刷新activity  通知activity界面跳转
            mView.retrursh();
        }
    }

//    public AllPresenter(AllContract.AllModel mModel, AllContract.AllView mView) {
//        super(mModel, mView);
//    }
//
//    //请求服务器token方法
//    public void requestToken() {
//        Map<String, Object> map = new HashMap<>();
//        if (ActivityCompat.checkSelfPermission(App.getInstance().getApplicationContext(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
//            return;
//        }
//        String imei = PhoneUtils.getIMEI();
//        map.put("imie",""+imei);
//        mModel.getTokent(map).subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(this::onNext);
//    }
//
//
//    @Override
//    public void onNext(@NonNull com.example.mvp.model.BaseEntity baseEntity) {
//        //请求成功逻辑校验
//        ResultBean entity = (ResultBean) baseEntity;
//        if (entity.getStatus().equals("200")){
//            //保存token到本地sp存储
//            SpUtils.saveData("token",entity.getValues().getToken());
//            //刷新activity  通知activity界面跳转
//            mView.retrursh();
//        }
//    }
}
